<!--
 * @FilePath: /web-vue/src/views/HouseType/addForm.vue
 * @Description: 【房源类型】新增表单
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="add-dialog"
      title="新增【房源类型】"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :fullscreen="false"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addForm"
        status-icon
        label-width="150px"
        style="width: 100%"
        class="layer-form-style"
      >
        <el-form-item label="类型：" prop="name" size="mini">
          <el-input v-model="addForm.name" placeholder="请输入类型" clearable style="width:99%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { DateTimeRange_Picker_Option, DateRange_Picker_Option, Date_Picker_Option } from "@/utils/datePicker";

export default {
  components: {
  },

  data() {
    return {
      visible: false,
      confirmLoading: false,
      addForm: {
        name: null,
        // 辅助属性
      },
      // 表单校验规则
      rules: {
        name: [
          {
            required: true,
            message: "类型不能为空！",
            trigger: "blur",
          },
        ],
      },
      // 日期选择快捷方式
      DateTimeRange_Picker_Option,
      DateRange_Picker_Option,
      Date_Picker_Option,
    };
  },

  methods: {
    // 打开【房源类型】表单新增窗口
    openAddForm(isShow) {
      this.visible = isShow;
    },

    // 关闭【房源类型】表单新增窗口
    handleCancel() {
      this.visible = false;
      this.$refs["addForm"].resetFields();

      // 清空上传文件列表
    },

    // 提交
    handleSubmit() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".add-dialog"),
            lock: true,
            text: "正在保存，请耐心等待！",
          });

          // 请求参数
          let param = Object.assign({}, this.addForm);



          // 发起请求
          this.$api
            .addHouseType(param)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit");
                this.handleCancel();
                // 提示信息
                this.$message({
                  type: "success",
                  message: `操作成功！已新增【房源类型】!${message}`,
                  duration: 1680,
                });
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【房源类型】新增", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch((err) => {
              loading.close();
              this.visible = false;
              console.log(`【房源类型】新增接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
